#include "netdisk3.h"

int pwd(char* working_dir, int curr_folder, MYSQL* mysql)
{
    char sql[4096];
    sprintf(sql, "select * from virtual_file where id = %d", curr_folder);
    int ret = mysql_query(mysql, sql);
    MYSQL_QUERY_CHECK(ret, mysql);

    MYSQL_RES* result = mysql_store_result(mysql);
    MYSQL_ROW row = mysql_fetch_row(result);

    memcpy(working_dir, row[4], strlen(row[4]));
    mysql_free_result(result);
    return 0;
}

void error_send(int ret, int netfd){
    char error1[] = "wrong argument!\n";
    int error_length = strlen(error1);
    if(ret == -1){
        send(netfd, &ret, sizeof(ret), MSG_NOSIGNAL);
        send(netfd, &error_length, sizeof(int), MSG_NOSIGNAL);
        send(netfd, error1, error_length, MSG_NOSIGNAL);
    }else{
        send(netfd, &ret, sizeof(ret), MSG_NOSIGNAL);
    }
}

void func_send(int ret, int netfd, char* msg){
    char error1[] = "wrong argument!\n";
    int error_length = strlen(error1);

    strcat(msg, "\n");
    int msg_length = strlen(msg);
    if(ret == -1){
        send(netfd, &ret, sizeof(ret), MSG_NOSIGNAL);
        send(netfd, &error_length, sizeof(int), MSG_NOSIGNAL);
        send(netfd, error1, error_length, MSG_NOSIGNAL);
    }else{
        send(netfd, &ret, sizeof(ret), MSG_NOSIGNAL);
        send(netfd, &msg_length, sizeof(int), MSG_NOSIGNAL);
        send(netfd, msg, msg_length, MSG_NOSIGNAL);
    }
}
